﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JSC.Portal.Web;
using JSC.Portal.Entities;
using JSC.Portal.Data;
using System.Web.Mvc;
using JSC.Core;
using JSC.Core.Data;
using JSC.Portal.Module;
using JSC.Portal.Services;
using System.Web.Routing;

namespace JSC.Portal.Web.App.Modules.StaticContents
{
    public class StaticContentModule : ModuleBase
    {
        private IDao<StaticContent> _dao;

        public StaticContentModule(Section section) : base(section) 
        {
            _dao = new CommonDao<StaticContent>();
        }

        public override void Load()
        {
            StaticContent = Get(Section.Id);
            Data.Id = StaticContent.Id;
        }

        public override void LoadById(int id)
        {
            Load();
        }

        public override void LoadByIdAndPath(int id, string path)
        {
            Load();
        }

        private StaticContent Get(int sectionid)
        {
            IDao<StaticContent> dao = new CommonDao<StaticContent>();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            string query = "from StaticContent sc where sc.Section.Id = :sectionid";
            parameters.Add("sectionid", sectionid);
            StaticContent result = dao.GetUniqueByQuery(query, parameters);
            if (result == null)
            {
                result = new StaticContent();
                ISectionService svc = FacadeFactory.CreateFacade<ISectionService>();
                result.Section = svc.GetById(sectionid, false);
            }
            return result;
        }

        public StaticContent StaticContent
        {
            get;
            private set;
        }

        public StaticContent SaveOrUpdate()
        {
            return _dao.SaveOrUpdate(StaticContent);
        }
    }
}
